package com.atguigu.headline.controller;
import com.atguigu.headline.pojo.NewsUser;
import com.atguigu.headline.pojo.Result;
import com.atguigu.headline.pojo.ResultCodeEnum;
import com.atguigu.headline.service.NewsUserService;
import com.atguigu.headline.service.impl.NewsUserServiceImpl;
import com.atguigu.headline.util.JwtHelper;
import com.atguigu.headline.util.MD5Util;
import com.atguigu.headline.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/*")
public class NewsUserController extends BaseController{
    private NewsUserService newsUserService =new NewsUserServiceImpl();
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsUser newsUser = WebUtil.readJson(req, NewsUser.class);
        NewsUser findnewsUser= newsUserService.findUser(newsUser.getUsername());
        Result result=null;
        if(findnewsUser!=null){
            if (findnewsUser.getUserPwd().equals(MD5Util.encrypt(newsUser.getUserPwd()))){
                Integer uid = findnewsUser.getUid();
                String token = JwtHelper.createToken(uid.longValue());
                Map map=new HashMap();
                map.put("token",token);
                result=Result.ok(map);
            }else{
                result=Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
            }
        }else{
            result= Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }
        WebUtil.writeJson(resp,result);
    }


    protected void getUserInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String token = req.getHeader("token");
        int i = JwtHelper.getUserId(token).intValue();
        NewsUser newsUser = newsUserService.getUserInfo(i);
        Result result=null;
        if(newsUser!=null){
            Map<String,NewsUser> map=new HashMap<>();
            map.put("loginUser",newsUser);
            result=Result.ok(newsUser);
        }else{
             result=Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        WebUtil.writeJson(resp,result);
    }



    protected void checkUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        NewsUser newsUser = newsUserService.findUser(username);
        if(newsUser!=null){
            WebUtil.writeJson(resp,Result.build(null,ResultCodeEnum.USERNAME_USED));
        }else{

            WebUtil.writeJson(resp,Result.ok(null));
        }
    }


    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsUser newsUser = WebUtil.readJson(req, NewsUser.class);
        NewsUser user = newsUserService.findUser(newsUser.getUsername());
        if (user==null){
            newsUserService.registUser(newsUser);
            WebUtil.writeJson(resp,Result.ok(null));
        }else{
            WebUtil.writeJson(resp,Result.build(null,ResultCodeEnum.USERNAME_USED));
        }
    }
}
